<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button type="primary" @click="searchHandle">查询</el-button>
      <el-button type="primary" @click="searchReset">清空</el-button>
    </el-button-group>
    <el-form inline @keyup.enter.native="searchHandle">
      <el-form-item label="发起部门">
        <el-input v-model="dataForm.deptId"></el-input>
      </el-form-item>
      <el-form-item label="维修项">
        <el-select v-model="dataForm.repairConfigId" placeholder="请选择">
          <el-option v-for="item in repairConfig" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="顾客姓名">
        <el-input v-model="dataForm.customerName"></el-input>
      </el-form-item>
    </el-form>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
        <el-table-column prop="repairId" label="订单号"></el-table-column>
        <el-table-column prop="deptName" label="发起部门"></el-table-column>
        <el-table-column prop="repairConfigId" label="维修项"></el-table-column>
        <el-table-column prop="customerName" label="顾客姓名"></el-table-column>
        <el-table-column prop="customerTel" label="联系电话"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="repairCheck(scope.row.id)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="avue-crud__pagination">
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        background
        layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <!-- 查看维修订单 -->
    <check-form v-if="checkVisible" ref="checkRepair" @refreshDataList="getDataList"></check-form>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { checkRepairLog } from '@/api/eims/repair'
  import { fetchRepairList } from '@/api/eims/repair-config'
  import { getDeptInfoByDeptTypes } from '@/api/admin/dept.js'
  
  import CheckForm from './CheckForm'
  export default {
    data () {
      return {
        activeName: 'first',
        dataForm: {},
        repairConfig: [],
        deptOptions: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        checkVisible: false
      }
    },
    components: {
      CheckForm
    },
    created () {
      this.getDataList()
      this.getRepairConfig()
      this.getDeptList()
    },
    computed: {
      ...mapGetters(['userInfo']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      searchHandle(){
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        checkRepairLog(Object.assign({ workDeptId: this.userInfo.lessDeptId, current: this.pageIndex, size: this.pageSize })).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        }).catch((err) => {
          this.dataListLoading = false
          this.$message.error(err);
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 清空表单
      searchReset(){
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
      },
      getRepairConfig(){
        fetchRepairList({current: 1, size: 100}).then(response => {
          if(response.data.code == '0'){
            this.repairConfig = response.data.data.records
          }
        }).catch((err) => {
          this.$message.error(err);
        })
      },
      // 获取维修加工部门
      getDeptList(){
        let data = {
          deptTypes : [2,8]
        }
        getDeptInfoByDeptTypes(data).then(result => {
          let dataList = result.data.data
          let options = []
          dataList.forEach(item => {
            options.push({value:item.id,name:item.name})
          })
          this.deptOptions = options
        })
      },
      // 查看维修订单
      repairCheck(id){
        this.checkVisible = true
        this.$nextTick(() => {
          this.$refs.checkRepair.init(id,this.repairConfig,this.deptOptions)
        })
      }
    }
  }
</script>

<style>
</style>
